﻿using System;
using System.Globalization;
using System.Windows.Forms;
using AutoplanetaTaskManager.Daos.EntityFramework;
using AutoplanetaTaskManager.Models;
using AutoplanetaTaskManager.Views.Components;

namespace AutoplanetaTaskManager.Views
{
    public partial class RemoveTaskForm : Form
    {
        private int _taskId;
        private Task _task;
        private PostItemComponent _parentPostItemComponent;

        public RemoveTaskForm(PostItemComponent postItemComponent, int taskId)
        {
            InitializeComponent();
            _parentPostItemComponent = postItemComponent;
            _taskId = taskId;
        }

        private void RemoveTaskForm_Load(object sender, EventArgs e)
        {
            _task = new TaskDao().Get(_taskId);
            if (_taskId != null)
            {
                DateTime dateTime = _task.TaskDateTime.GetValueOrDefault();

                clientNameTextBox.Text = _task.ClientName;
                postNameTextBox.Text = new PostDao().Get((int) _task.PostId).Name ?? "";
                dateTextBox.Text = string.Format("{0:dd.MM.yyyy}", dateTime);
                timeTextBox.Text = string.Format("{0:HH:mm}", dateTime);
                countHoursTextBox.Text = _task.CountHours.Value.ToString(CultureInfo.InvariantCulture);
                finishedTextBox.Text = _task.Finished != null && (bool) _task.Finished ? "ДА" : "НЕТ";
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            if (_task!=null)
            {
                new TaskDao().Remove(_taskId);
                _parentPostItemComponent.UpdateContent();
            }
            Dispose();
        }
    }
}
